home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / p_misc / 7p203upp.exe / V203SRC.LZH / UNIX.C < prev    next >
C/C++ Source or Header  |  1992-06-29  |  6KB  |  228 lines

  1. /*--------------------------------*\
  2. | Additions for UNIX-compatibility |
  3. \*--------------------------------*/
  4. #ifdef __unix__
  5.  
  6.  #include "7plus.h"
  7.  
  8.  
  9.  #ifdef __vax__
  10.   char *strdup (const char *s1)
  11.   { char *s;
  12.  
  13.     s = malloc (strlen(s1) + 1);
  14.     strcpy (s , s1);
  15.     return (s);
  16.   }
  17.  #endif /* __vax__ */
  18.  
  19.  static int first = 1;
  20.  
  21.  int my_getch()
  22.  {
  23.    unsigned char c;
  24.    int fd;
  25.  
  26.    fd = fileno (stdin);
  27.    if (first)
  28.    {
  29.      first = 0;
  30.     #ifdef SYSV
  31.      (void) ioctl(fd, TCGETA, (char *) &sg[OFF]);
  32.     #else
  33.      (void) gtty(fd, &sg[OFF]);
  34.     #endif
  35.      sg[ON] = sg[OFF];
  36.  
  37.     #ifdef SYSV
  38.      sg[ON].c_lflag &= ~(ICANON|ECHO);
  39.      sg[ON].c_cc[VMIN] = 1;
  40.      sg[ON].c_cc[VTIME] = 0;
  41.     #else
  42.      sg[ON].sg_flags &= ~(ECHO | CRMOD);
  43.      sg[ON].sg_flags |= CBREAK;
  44.     #endif
  45.    }
  46.  
  47.   #ifdef SYSV
  48.    (void) ioctl(fd, TCSETAW, (char *) &sg[ON]);
  49.   #else
  50.    (void) stty(fd, &sg[ON]);
  51.   #endif
  52.  
  53.    read(fd, &c, 1);
  54.  
  55.   #ifdef SYSV
  56.    (void) ioctl(fd, TCSETAW, (char *) &sg[OFF]);
  57.   #else
  58.    (void) stty(fd, &sg[OFF]);
  59.   #endif
  60.  
  61.    return (int) c;
  62.  }
  63.  
  64.  #ifdef __i386__
  65.   #define MAXCMD 1024
  66.  
  67.   int rename (const char *s1, const char *s2)
  68.   {
  69.     char tmp[MAXCMD];
  70.  
  71.     (void) sprintf(tmp, "mv %s %s", s1, s2);
  72.     return (system(tmp));
  73.   }
  74.  
  75.   #ifndef _HAVE_STRSTR
  76.    /*
  77.      strstr - public-domain implementation of standard C library function
  78.  
  79.      last edit:  02-Sep-1990  D A Gwyn
  80.  
  81.      This is an original implementation based on an idea by D M Sunday,
  82.      essentially the "quick search" algorithm described in CACM V33 N8.
  83.      Unlike Sunday's implementation, this one does not wander past the
  84.      ends of the strings (which can cause malfunctions under certain
  85.      circumstances), nor does it require the length of the searched
  86.      text to be determined in advance.  There are numerous other subtle
  87.      improvements too.  The code is intended to be fully portable, but in
  88.      environments that do not conform to the C standard, you should check
  89.      the sections below marked "configure as required".  There are also
  90.      a few compilation options, as follows:
  91.  
  92.      #define ROBUST  to obtain sane behavior when invoked with a null
  93.          pointer argument, at a miniscule cost in speed
  94.      #define ZAP  to use memset() to zero the shift[] array; this may
  95.          be faster in some implementations, but could fail on
  96.          unusual architectures
  97.      #define DEBUG  to enable assertions (bug detection)
  98.    */
  99.    #define ROBUST
  100.    #define ZAP
  101.  
  102.    #ifdef __STDC__
  103.     #include  <limits.h>    /* defines UCHAR_MAX */
  104.  
  105.     #ifdef ZAP
  106.      typedef void  *pointer;
  107.      extern pointer  memset( pointer, int, size_t );
  108.     #endif
  109.  
  110.    #else  /* normal UNIX-like C environment assumed; configure as required: */
  111.  
  112.     typedef unsigned  size_t;  /* type of result of sizeof */
  113.  
  114.     #ifndef NULL
  115.      #define  NULL    0    /* null pointer constant */
  116.     #endif
  117.  
  118.     #define  UCHAR_MAX  255    /* largest value of unsigned char */
  119.                                /* 255 @ 8 bits, 65535 @ 16 bits  */
  120.  
  121.     #ifdef ZAP
  122.      typedef char  *pointer;
  123.      extern pointer  memset();
  124.     #endif
  125.  
  126.     #define const  /* nothing */
  127.  
  128.    #endif  /* __STDC__ */
  129.  
  130.    #ifndef DEBUG
  131.     #define  NDEBUG
  132.    #endif
  133.  
  134.    #include <assert.h>
  135.  
  136.    typedef const unsigned char  cuc;  /* char variety used in algorithm */
  137.  
  138.    #define EOS  '\0'      /* C string terminator */
  139.  
  140.    char *          /* returns -> leftmost occurrence,
  141.                       or null pointer if not present */
  142.    strstr( s1, s2 )
  143.      const char  *s1;       /* -> string to be searched */
  144.      const char  *s2;       /* -> search-pattern string */
  145.    {
  146.      register cuc  *t;      /* -> text character being tested */
  147.      register cuc  *p;      /* -> pattern char being tested */
  148.      register cuc  *tx;     /* -> possible start of match */
  149.      register size_t  m;    /* -> length of pattern */
  150.      register cuc  *top;    /* -> high water mark in text */
  151.    #if UCHAR_MAX > 255      /* too large for auto allocation */
  152.      static        /* not malloc()ed; that can fail! */
  153.    #endif          /* else allocate shift[] on stack */
  154.        size_t  shift[UCHAR_MAX + 1];  /* pattern shift table */
  155.  
  156.    #ifdef ROBUST        /* not required by C standard */
  157.      if ( s1 == NULL || s2 == NULL )
  158.        return NULL;    /* certainly, no match is found! */
  159.    #endif
  160.  
  161.      /* Precompute shift intervals based on the pattern;
  162.         the length of the pattern is determined as a side effect: */
  163.  
  164.    #ifdef ZAP
  165.      (void)memset( (pointer)&shift[1], 0, UCHAR_MAX * sizeof(size_t) );
  166.    #else
  167.      {
  168.      register unsigned char  c;
  169.  
  170.      c = UCHAR_MAX;
  171.      do
  172.        shift[c] = 0;
  173.      while ( --c > 0 );
  174.      }
  175.    #endif /* ZAP */
  176.      /* Note: shift[0] is undefined at this point (fixed later). */
  177.  
  178.      for ( m = 1, p = (cuc *)s2; *p != EOS; ++m, ++p )
  179.        shift[(cuc)*p] = m;
  180.  
  181.      assert(s2[m - 1] == EOS);
  182.  
  183.      {
  184.      register unsigned char  c;
  185.  
  186.      c = UCHAR_MAX;
  187.      do
  188.        shift[c] = m - shift[c];
  189.      while ( --c > 0 );
  190.  
  191.      /* Note: shift[0] is still undefined at this point. */
  192.      }
  193.  
  194.      shift[0] = --m;    /* shift[EOS]; important details! */
  195.  
  196.      assert(s2[m] == EOS);
  197.  
  198.      /* Try to find the pattern in the text string: */
  199.  
  200.      for ( top = tx = (cuc *)s1; ; tx += shift[*(top = t)] )
  201.      {
  202.        for ( t = tx, p = (cuc *)s2; ; ++t, ++p )
  203.        {
  204.          if ( *p == EOS )       /* entire pattern matched */
  205.            return (char *)tx;
  206.  
  207.          if ( *p != *t )
  208.            break;
  209.        }
  210.  
  211.        if ( t < top )    /* idea due to ado@elsie.nci.nih.gov */
  212.          t = top;  /* already scanned this far for EOS */
  213.  
  214.        do
  215.        {
  216.          assert(m > 0);
  217.          assert(t - tx < m);
  218.  
  219.          if ( *t == EOS )
  220.            return NULL;  /* no match */
  221.        }
  222.        while ( ++t - tx != m );  /* < */
  223.      }
  224.    }
  225.   #endif /* ifndef _HAVE_STRSTR */
  226.  #endif /* ifdef __i386__      */
  227. #endif /* ifdef __unix__      */
  228.